﻿using UnityEngine;
using System.Collections;

public class AchievementManager : MonoBehaviour {

    //Achievements
    public Achievement EnemyKill1;
    public Achievement EnemyKill2;
    public Achievement EnemyKill3;
    public Achievement EnemyKill4;

    public Achievement LevelTotalWin1;
    public Achievement LevelTotalWin2;

    public Achievement LevelTotalLost1;
    public Achievement LevelTotalLost2;

    public Achievement SpecificLevelWin1;
    public Achievement SpecificLevelWin2;

    public Achievement XpGain1;

    public Achievement TotalUpgradeBought1;

    public Achievement MaxLevelUpgrade;

    //Count variables
    private int nbUpgradeBought;
    private int nbEnemyKilled;
    private int nbLostLife;
    private int nbLevelPlayed;
    private int[] nbLevelWon;
    private int nbLevelLost;
    private int nbXpGain;

    [SerializeField]
    private int levelCount;
    public int LevelCount
    {
        get { return levelCount; }
        set { levelCount = value; }
    }

	// Use this for initialization
	void Start () {
        nbUpgradeBought = 0;
        nbXpGain = 0;
        nbLevelLost = 0;
        nbEnemyKilled = 0;
        nbLostLife = 0;
        nbLevelPlayed = 0;
        nbLevelWon = new int[levelCount];
        EventManager<GameObject>.AddListener("ennemy reach base", onEnemyReachBase);
        EventManager<GameObject>.AddListener("ennemy killed", onEnemyKilled);
        EventManager.AddListener("start level", onStartLevel);
        EventManager<EndLevelStat>.AddListener("end level", onEndLevel);
        EventManager<int>.AddListener("end level", onXpGain);
        EventManager<Upgrade>.AddListener("buy upgrade", onUpgradeBuy);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    private void onEnemyReachBase(GameObject enemy)
    {
        nbLostLife++;
    }

    private void onEnemyKilled(GameObject enemy)
    {
        nbEnemyKilled++;
        tryToTrigger(EnemyKill1, nbEnemyKilled);
        tryToTrigger(EnemyKill2, nbEnemyKilled);
        tryToTrigger(EnemyKill3, nbEnemyKilled);
        tryToTrigger(EnemyKill4, nbEnemyKilled);
    }

    private void onEndLevel(EndLevelStat stat)
    {
        if(stat.Win)
        {
            nbLevelWon[stat.Level.Id]++;
            int sum = 0;
            for(int index = 0; index < nbLevelWon.Length; ++index)
            {
                sum += nbLevelWon[index];
                tryToTrigger(SpecificLevelWin1, nbLevelWon[index]);
                tryToTrigger(SpecificLevelWin2, nbLevelWon[index]);
            }
            tryToTrigger(LevelTotalWin1, sum);
        }
        else
        {
            nbLevelLost++;
            tryToTrigger(LevelTotalLost1, nbLevelLost);
            tryToTrigger(LevelTotalLost2, nbLevelLost);
        }
    }

    private void onStartLevel()
    {
        nbLevelPlayed++;
    }

    private void onXpGain(int gain)
    {
        nbXpGain += gain;
        tryToTrigger(XpGain1, nbXpGain);
    }

    private void onUpgradeBuy(Upgrade upgrade)
    {
        nbUpgradeBought++;
        tryToTrigger(TotalUpgradeBought1, nbUpgradeBought);
        tryToTrigger(MaxLevelUpgrade, upgrade.isLevelMax());
    }

    private void tryToTrigger(Achievement achievement, int amount)
    {
        if(amount >= achievement.AmountToTrigger && !achievement.Triggered)
        {
            achievement.trigger();
        }
    }

    private void tryToTrigger(Achievement achievement, bool trigger)
    {
        if (trigger)
        {
            achievement.trigger();
        }
    }

}
